<template>
  <div class="login-page">
    <div class="pic">
      <img src="../assets/logobxg.png" alt="">
      <span>• IT资讯</span>
    </div>
<!-- 头部 -->
   <div class="login-head">
      <h3>欢迎登录</h3>
    </div>
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="[
          { required: true },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
        ]"
      />
      <van-field
        v-model="code"
        :type="show ? 'text' : 'password'"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true },
          { pattern: /^\d{6}$/, message: '密码至少包含6个字符' }
        ]"
      >
      <template #button>
        <van-icon @click="code=''" :name="show?'close':''" />
        <van-icon @click='onShow' :name="show?'eye-o':'closed-eye'" />
     </template>
      </van-field>
      <div  style="margin: 15px">
        <van-button block type="info" native-type="submit" @click='onLoading' :class="mobile&&code?'':'active'">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      mobile: '',
      code: '',
      show: false
    }
  },

  methods: {
    async onSubmit () {
      // console.log('submit', values)
      const res = await login({
        mobile: this.mobile,
        code: this.code
      })
      console.log(res)
      // 登录之前一定要存token
      setToken(res.data.token)
      setTimeout(() => {
        this.$router.push('/article')
        this.$toast.success('登录成功')
      }, 1500)
    },
    onShow () {
      this.show = !this.show
    },
    onLoading () {
      this.$toast.loading({
        message: '登录中',
        forbidClick: true,
        loadingType: 'spinner'
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login{
    &-page {
    padding-top: 35px;
    background-image: url('../assets/bg.png');
  }
  &-head {
    display: flex;
    padding: 20px 30px 35px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-size: 24px;
      font-weight: normal;
      padding-left: 10px;
    }
}}
.van-form {
  padding: 0 30px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;}}

 .van-icon{
  font-size: 20px;
  margin-left: 8px;
 }
 .pic{
  display: flex;
align-items: center;
  height: 30px;
  padding-left: 23px;
 img{
  width: 100px;
 }
 span{
  font-size: 18px;
  margin-left: 5px;
 }
 }
 .van-button{
border-radius: 5px;
 }
.van-button.active{
  background-color: #94BFFF;
 }
</style>
